import axios from 'axios'
import JSONBig from 'json-bigint'

import router from '@/router';

const JSONbigString = JSONBig({ storeAsString: true }); 

const service = axios.create({
  timeout: 10000,
  baseURL:"/api",
  transformResponse:[
    function(data){
      try {
        const res = JSONbigString.parse(data);
        return res
      } catch (error) {
        return JSON.parse(data)
      }
    }
  ]
})

service.interceptors.request.use(
  (config) => {
    config.headers.token = localStorage.getItem('chatwoot-token') || ''
    return config
  },
  (err) => {
    return Promise.reject(err)
  }
)

service.interceptors.response.use((res) => {
    console.log(res)
    const code = res.data.code
    if(code == 200){
      return res.data.data
    }else if(code == 401 || code == 420){
      
    } 
    else {
      const message = res.data.msg
      return Promise.reject(message)
    }
  
  },
  (error) => {
    return Promise.reject(error)
  }
)
export default service
